package com.ljy.jymail.order.config;



import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMqConfig {

//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
//        System.out.println("收到过期的订单信息,准备关闭订单"+entity.getOrderSn());
//        //由于配置文件中配置了手动确认，所以需要用通道进行确认
//        //收到消息后，通知
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }

    /*容器中的Binding，Queue，Exchange 都会自会创建(RabbitMQ没有的情况)
    * 项目一旦创建好队列，rabbitMq只要有，即使属性发生了变化，队列也不会进行覆盖
    * */

    //队列一:死信队列 (延迟队列)
    @Bean
    public Queue orderDelayQueue(){

        //x-dead-Letter-exchange: order-event-exchange 死信路由
        //x-dead-Letter-routing-key: order.release.order 死信路由键
        //x-message-ttl: 60000
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",60000); //存活时间

        //String name, boolean durable 是否持久化, boolean exclusive 是否排他,
        // boolean autoDelete 自动删除, @Nullable Map<String, Object> arguments 自定义属性
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments);

        return queue;
    }

    //队列二:普通队列 被消费者监听
    //order.release.order.queue
    @Bean
    public Queue orderReleaseOrderQueue(){
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        return queue;
    }

    //交换机
    @Bean
    public Exchange orderEventExchange(){
        /*
        * String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        * */
        return new DirectExchange("order-event-exchange",true,false);
    }


    /*
    * 交换机与队列的绑定关系
    * */
    @Bean
    public Binding orderCreateOrderBinding(){
        /*
        * String destination, Binding.DestinationType destinationType, String exchange,
        * String routingKey, @Nullable Map<String, Object> arguments
        * */
       return new Binding("order.delay.queue",
                 Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null
                );
    }


    @Bean
    public Binding orderReleaseOrder(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null
        );
    }


    @Bean
    public Binding OrderReleaseOtherBinding(){
        /*
        * 将订单解锁与库存解锁进行绑定
        * */
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null
        );
    }

    /*
    * 秒杀服务队列
    * */
    @Bean
    public Queue orderSeckillOrderQueue(){
        return new Queue("order.seckill.order.queue",true,false,false);
    }



    /*
    * 绑定秒杀服务
    * */
    @Bean
    public Binding OrderSeckillBinding(){
        /*
         * 将订单解锁与库存解锁进行绑定
         * */
        return new Binding(
                "order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null
        );
    }
}
